package com.sch.www.ele.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.sch.www.codeGenerator.base.CustomPage;
import com.sch.www.common.base.BaseController;
import com.sch.www.common.base.ResultBean;
import com.sch.www.common.constData.ConstNumber;
import com.sch.www.common.util.IdCardUtil;
import com.sch.www.common.validator.EmailFluentValidator;
import com.sch.www.common.validator.IdCardFluentValidator;
import com.sch.www.common.validator.MobileFluentValidator;
import com.sch.www.common.validator.NotBlankFluentValidator;
import com.sch.www.ele.entity.CareUser;
import com.sch.www.ele.services.CareUserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/v1/careUser")
@RestController
public class CareUserController extends BaseController {
    @Autowired
    CareUserServices careUserServices;

    @RequestMapping("/accountId/{accountId}")
    public ResultBean<CareUser> getByAccountId(@PathVariable String accountId) {
        CareUser careUser = careUserServices.getByAccountId(accountId);
        ResultBean<CareUser> resultBean = ResultBean.newInstance();
        return resultBean.setData(careUser);
    }

    @RequestMapping(value = "list")
    public  ResultBean<CustomPage<CareUser>> list(
            String name,
            String idCard,
            String phoneNumber,
            String email,
            Byte gender,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        ResultBean<CustomPage<CareUser>> resultBean = ResultBean.newInstance();
        CustomPage<CareUser> page = careUserServices.list(name, idCard, email, phoneNumber, gender, pageNum, pageSize);
        resultBean.setSuccess().setData(page);
        return resultBean;
    }

    @PostMapping("add")
    public ResultBean<CareUser> add(@RequestBody  CareUser careUser) {
        ResultBean<CareUser> resultBean = ResultBean.newInstance();
        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(careUser.getName(), new NotBlankFluentValidator("name"))
                .on(careUser.getIdCard(), new IdCardFluentValidator("idCard"))
                .on(careUser.getPhoneNumber(), new MobileFluentValidator("phoneNumber"))
                .on(careUser.getEmail(), new EmailFluentValidator(false))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        careUser.setGender(IdCardUtil.getGenderByIdCard(careUser.getIdCard()) == 1 ? ConstNumber.BYTE_1 : ConstNumber.BYTE_0);
        careUser.setBirthday(IdCardUtil.getBirthByIdCard(careUser.getIdCard()));
        careUserServices.insertSelective(careUser);
        return resultBean;
    }


    @PostMapping("modify")
    public ResultBean<CareUser> modify(@RequestBody  CareUser careUser) {
        ResultBean<CareUser> resultBean = ResultBean.newInstance();
        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(careUser.getId(), new NotBlankFluentValidator("id"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        careUserServices.updateByIdSelective(careUser);
        return resultBean;
    }


}
